﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CIS.Client.Common.Popups
{
    /// <summary>
    /// PopupMessageWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PopupMessageWindow : Window
    {
        public PopupMessageWindow()
        {
            InitializeComponent();
        }
        private static BitmapImage NormalInfoImg = new BitmapImage(new Uri("/BubblePrompt;component/Images/ic_pop_normal.png", UriKind.RelativeOrAbsolute));
        private static BitmapImage WarnInfoImg = new BitmapImage(new Uri("/BubblePrompt;component/Images/ic_pop_warn.png", UriKind.RelativeOrAbsolute));
        private static BitmapImage ErrorInfoImg = new BitmapImage(new Uri("/BubblePrompt;component/Images/ic_pop_fail.png", UriKind.RelativeOrAbsolute));

        private static Brush NormalBackColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DEF4FF"));
        private static Brush WarnBackColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFF0E6"));
        private static Brush ErrorBackColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFF0E6"));

        private static Brush NormalInfoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0094FF"));
        private static Brush WarnInfoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FB9048"));
        private static Brush ErrorInfoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FB9048"));
        System.Windows.Threading.DispatcherTimer uiTimer;
        public FrameworkElement Element
        {
            get { return (FrameworkElement)GetValue(ElementProperty); }
            set { SetValue(ElementProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Element.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElementProperty =
            DependencyProperty.Register("Element", typeof(FrameworkElement), typeof(Window), new PropertyMetadata(null));



        public string Info
        {
            get { return (string)GetValue(InfoProperty); }
            set { SetValue(InfoProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Info.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InfoProperty =
            DependencyProperty.Register("Info", typeof(string), typeof(Window), new PropertyMetadata(""));



        public int Delay
        {
            get { return (int)GetValue(DelayProperty); }
            set { SetValue(DelayProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Delay.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DelayProperty =
            DependencyProperty.Register("Delay", typeof(int), typeof(Window), new PropertyMetadata(3));

        public Brush BackColor
        {
            get { return (Brush)GetValue(BackColorProperty); }
            set { SetValue(BackColorProperty, value); }
        }
        // Using a DependencyProperty as the backing store for BackColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColor", typeof(Brush), typeof(Window), new PropertyMetadata(NormalBackColor));

        public Brush InfoTextColor
        {
            get { return (Brush)GetValue(InfoTextColorProperty); }
            set { SetValue(InfoTextColorProperty, value); }
        }
        // Using a DependencyProperty as the backing store for BackColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InfoTextColorProperty =
            DependencyProperty.Register("InfoTextColor", typeof(Brush), typeof(Window), new PropertyMetadata(NormalInfoColor));


        /// <summary>
        /// 0:Normal 1:Warn 2:Error
        /// </summary>
        public int InfoType
        {
            get { return (int)GetValue(InfoTypeProperty); }
            set
            {
                SetValue(InfoTypeProperty, value);
                if (value == 0)
                {
                    ImageSource = NormalInfoImg;
                    BackColor = NormalBackColor;
                    InfoTextColor = NormalInfoColor;
                }
                else if (value == 1)
                {
                    ImageSource = WarnInfoImg;
                    BackColor = WarnBackColor;
                    InfoTextColor = WarnInfoColor;
                }
                else if (value == 2)
                {
                    ImageSource = ErrorInfoImg;
                    BackColor = ErrorBackColor;
                    InfoTextColor = ErrorInfoColor;
                }
            }
        }

        // Using a DependencyProperty as the backing store for InfoType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InfoTypeProperty =
            DependencyProperty.Register("InfoType", typeof(int), typeof(Window), new PropertyMetadata(0));



        public BitmapImage ImageSource
        {
            get { return (BitmapImage)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(BitmapImage), typeof(Window), new PropertyMetadata(null));


        DateTime startTime;

        private void UITimerTick(object sender, EventArgs e)
        {
            if (popupInfo.IsOpen == false)
            {
                uiTimer.Stop();
                return;
            }
            if ((DateTime.Now - startTime).TotalSeconds >= Delay)
            {
                this.Hide();
                popupInfo.IsOpen = false;
            }
        }

        private void popupInfo_Opened(object sender, EventArgs e)
        {
            if (uiTimer == null || uiTimer.IsEnabled == false)
            {
                uiTimer = new System.Windows.Threading.DispatcherTimer();
                uiTimer.Interval = TimeSpan.FromMilliseconds(1000);
                uiTimer.Tick += UITimerTick;
                startTime = DateTime.Now;
                uiTimer.Start();
            }
            else
            {
                startTime = DateTime.Now;
            }
        }

    }
}
